Imports System.Data.SqlClient

Friend Class DBScanLogin
    Inherits DBBase

    Private moServer As SQLAutoDocLib.Server
    Private moDatabase As SQLAutoDocLib.Database

    Public Event ItemScanStart(ByVal lServerID As Long, ByVal lDatabaseID As Long, ByVal lSessionID As Long, ByVal lLoginID As Long)
    Public Event ItemScanComplete(ByVal lServerID As Long, ByVal lDatabaseID As Long, ByVal lSessionID As Long, ByVal lLoginID As Long)

    Public Sub New( _
                ByVal oServer As SQLAutoDocLib.Server, _
                ByVal oDatabase As SQLAutoDocLib.Database)

        moServer = oServer
        moDatabase = oDatabase
    End Sub

    Public Function Scan(ByVal lSessionID As Long) As Boolean
        Dim bChangesFound As Boolean = False

        Dim oFactory As New Login_Factory
        Dim oAllLogins As Collection = oFactory.GetAllLoginsForDatabase(moDatabase.ID)

        For Each oLogin As SQLAutoDocLib.Login In oAllLogins
            oLogin.IsDeleted = True
            oLogin.Save()
        Next

        Dim iAlteredLogins As Integer = 0
        Dim bProcsAddedOrUpdated As Boolean = False
        RefreshLoginsForDatabase(oAllLogins, iAlteredLogins)
        If iAlteredLogins > 0 Then bProcsAddedOrUpdated = True

        If bProcsAddedOrUpdated = False Then
            '---Were any logins deleted?
            For Each oLogin As SQLAutoDocLib.Login In oAllLogins
                RaiseEvent ItemScanStart(moDatabase.ServerID, moDatabase.ID, lSessionID, oLogin.ID)

                If oLogin.IsDeleted = True Then
                    oLogin.DatabaseRevID = moDatabase.RevID
                    oLogin.Save(bForceAdd:=True)
                    bChangesFound = True
                End If

                RaiseEvent ItemScanComplete(moDatabase.ServerID, moDatabase.ID, lSessionID, oLogin.ID)
            Next
        Else
            bChangesFound = True
        End If

        Return bChangesFound
    End Function

    Private Sub RefreshLoginsForDatabase( _
                    ByRef oAllLogins As Collection, _
                    ByRef iAlteredLogins As Integer)

        Dim oFactory As New Login_Factory

        Dim sSQL As String = "select"
        sSQL = sSQL & "     a.uid,"
        sSQL = sSQL & "     a.name,"
        sSQL = sSQL & "     a.isntuser"
        sSQL = sSQL & " from "
        sSQL = sSQL & "     dbo.sysusers a"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     islogin = 1"
        sSQL = sSQL & "     and hasdbaccess=1"

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL, moDatabase.DecryptedConnectionString)

        For Each oRow As DataRow In oData.Rows
            Dim oLogin As SQLAutoDocLib.Login = oFactory.GetLoginByName(moDatabase.ID, oRow.Item("name"))

            If oLogin Is Nothing Then
                '---Found a new login
                oLogin = New SQLAutoDocLib.Login
                With oLogin
                    .Login = oRow.Item("name")
                    .UID = oRow.Item("uid")
                    .DatabaseID = moDatabase.ID
                    .DatabaseRevID = moDatabase.RevID
                    .UseIntegratedSecurity = oRow.Item("IsNTUser")
                End With

                iAlteredLogins = iAlteredLogins + 1
            End If

            If oAllLogins.Contains(CStr(oLogin.ID)) Then
                oAllLogins(CStr(oLogin.ID)).IsDeleted = False
            End If

            oLogin.IsDeleted = False

            oLogin.Save()
        Next
    End Sub
End Class
